var express = require('express');
var router = express.Router();
// 引入封装的 mysql模块
const mysql = require('../util/mysql/mysql');
const sql = require('../util/mysql/sql');
//用户名
router.post('/selName', function (req, res, next) {
  mysql.blog(sql.sel_name, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//用户信息
router.post('/selUser', function (req, res, next) {
  mysql.blog(sql.selUser, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//修改用户信息
router.post('/updateUser', function (req, res, next) {
  mysql.blog(sql.updateUser, [
    req.body.user_header,
    req.body.nickname,
    req.body.sex,
    req.body.birthday,
    req.body.user_name,
    req.body.user_id
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '用户信息修改成功了' }));
  });
});
//账户
router.post('/SelMoney', function (req, res, next) {
  mysql.blog(sql.SelMoney, [req.body.id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//加钱
router.post('/addMoney', function (req, res, next) {
  mysql.blog(sql.addMoney, [req.body.money, req.body.id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '您的外挂已到账！', money: req.body.money }));
  });
});
//账单
router.post('/selBill', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 10;
  let end = page * 10;
  mysql.blog(sql.selBill, [req.body.user_id], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//添加账单
router.post('/addBill', function (req, res, next) {
  mysql.blog(sql.addBill, [
    req.body.bill_type,
    req.body.consume_money,
    req.body.balance,
    req.body.consume_time,
    req.body.user_id
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '添加账单成功' }));
  });
});
//添加积分商城账单
router.post('/addBill/integral', function (req, res, next) {
  mysql.blog(sql.addBillIntegral, [
    req.body.bill_type,
    req.body.consume_money,
    req.body.balance,
    req.body.consume_time,
    req.body.user_id
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '添加账单成功' }));
  });
});
//积分订单
router.post('/selIndent/integral', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 6;
  let end = page * 6;
  mysql.blog(sql.SelIndentIntegral, [req.body.user_id], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
// 删除积分订单
router.post('/delIndent/integral', function (req, res, next) {
  mysql.blog(sql.delIndentIntegral, [req.body.indent_id], function (err, rows, fields) {
    if (err) console.log(err);
    res.send(JSON.stringify({ code: 1, msg: "删除成功" }));
  });
});
//订单(分页)
router.post('/selIndents', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 6;
  let end = page * 6;
  mysql.blog(sql.SelIndent, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//订单(全部)
router.post('/selIndent', function (req, res, next) {
  mysql.blog(sql.SelIndent, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//添加订单
router.post('/addIndent', function (req, res, next) {
  mysql.blog(sql.addIndent, [
    req.body.order_time,
    req.body.store_name,
    req.body.order_status,
    req.body.total_price,
    req.body.freight,
    req.body.user_id,
    req.body.product_id,
    req.body.order_evaluate,
    req.body.product,
    req.body.address_id,
    req.body.address_name,
    req.body.address,
    req.body.address_phone,
    req.body.boss_name,
    req.body.boss_phone
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '已添加到订单' }));
  });
});
// 添加积分商城订单
router.post('/addIntegral/indent', function (req, res, next) {
  mysql.blog(sql.addIntegralIndent, [
    req.body.order_time,
    req.body.store_name,
    req.body.order_status,
    req.body.total_price,
    req.body.freight,
    req.body.user_id,
    req.body.integral_id,
    req.body.order_evaluate,
    req.body.product,
    req.body.address_id,
    req.body.address_name,
    req.body.address,
    req.body.address_phone,
    req.body.boss_name,
    req.body.boss_phone
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '已添加到订单' }));
  });
});

//删除订单
router.post('/delOrder', function (req, res, next) {
  mysql.blog(sql.delOrder, [req.body.order_id], function (err, rows, fields) {
    if (err) { console.log(err); };
    res.send(JSON.stringify({ code: 1, msg: '订单删除成功！' }));
  });
});
//查询需要付款的订单
router.post('/selSingleIndent', function (req, res, next) {
  if (req.body.store) {
    mysql.blog(sql.SelSingleIndent, [req.body.user_id, req.body.order_time, req.body.store], function (err, rows, fields) {
      if (err) {
        console.log(err);
      };
      res.send(JSON.stringify(rows));
    });
  } else {
    mysql.blog(sql.SelSingleIndents, [req.body.user_id, req.body.order_time], function (err, rows, fields) {
      if (err) {
        console.log(err);
      };
      res.send(JSON.stringify(rows));
    });
  }
});
// TODO: 查询需要付款的积分订单
router.post('/selSingleIndent/integral', function (req, res, next) {
  if (req.body.store) {
    mysql.blog(sql.SelSingleIntegral, [req.body.user_id, req.body.order_time, req.body.store], function (err, rows, fields) {
      if (err) {
        console.log(err);
      };
      res.send(JSON.stringify(rows));
    });
  } else {
    mysql.blog(sql.SelSingleIntegrals, [req.body.user_id, req.body.order_time], function (err, rows, fields) {
      if (err) {
        console.log(err);
      };
      res.send(JSON.stringify(rows));
    });
  }
});
//订单状态
router.post('/indentStatus', function (req, res, next) {
  mysql.blog(sql.indentStatus, [req.body.order_status, req.body.orderId], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '已改为待发货' }));
  });
});
// 积分商城订单状态
router.post('/integralStatus', function (req, res, next) {
  mysql.blog(sql.integralStatus, [req.body.order_status, req.body.indent_id], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '已改为待发货' }));
  });
});
//添加评价
router.post('/addEvaluate', function (req, res, next) {
  mysql.blog(sql.addEvaluate, [
    req.body.evaluate_text,
    req.body.user_name,
    req.body.user_header,
    req.body.user_score,
    req.body.product_id,
    req.body.order_id,
    req.body.evaluate_time
  ], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '评价成功' }));
  });
});
//查看地址
router.post('/selAddress', function (req, res, next) {
  mysql.blog(sql.SelAddress, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//重置默认地址
router.post('/resetAddress', function (req, res, next) {
  mysql.blog(sql.resetAddress, [req.body.default_address, req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '默认地址已清空！' }));
  });
});
//设置默认地址
router.post('/defaultAddress', function (req, res, next) {
  console.log(req.body);
  mysql.blog(sql.defaultAddress, [req.body.default_address, req.body.address_id], function (err, rows, fields) {
    if (err) { console.log(err); };
    res.send(JSON.stringify({ msg: '默认地址已设置！' }));
  });
});
//添加地址
router.post('/addAddress', function (req, res, next) {
  mysql.blog(sql.addAddress, [
    req.body.consignee,
    req.body.area,
    req.body.street,
    req.body.contact_phone,
    req.body.comment,
    req.body.user_id,
    req.body.default_address
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '地址添加成功！', }));
  });
});
//编辑地址
router.post('/updateAddress', function (req, res, next) {
  mysql.blog(sql.updateAddress, [
    req.body.consignee,
    req.body.area,
    req.body.street,
    req.body.contact_phone,
    req.body.comment,
    req.body.user_id,
    req.body.default_address,
    req.body.address_id
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '地址修改成功！' }));
  });
});

//删除地址
router.post('/delAddress', function (req, res, next) {
  mysql.blog(sql.delAddress, [req.body.address_id], function (err, rows, fields) {
    if (err) { console.log(err); };
    res.send(JSON.stringify({ msg: '地址删除成功！' }));
  });
});
//查看收藏
router.post('/selCollect', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 3;
  let end = page * 3;
  mysql.blog(sql.SelCollect, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//添加收藏
router.post('/addCollect', function (req, res, next) {
  console.log(req.body);
  mysql.blog(sql.addCollect, [
    req.body.product_id,
    req.body.commodity,
    req.body.commodity_img,
    req.body.introduce,
    req.body.original_price,
    req.body.current_price,
    req.body.user_id
  ], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify({ msg: '收藏成功' }));
  });
});
//删除收藏
router.post('/delCollect', function (req, res, next) {
  mysql.blog(sql.delCollect, [req.body.product_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '取消收藏' }));
  });
});
//添加浏览记录
router.post('/addBrowsing', function (req, res, next) {

  mysql.blog(sql.addBrowsing, [
    req.body.product_id,
    req.body.commodity,
    req.body.commodity_img,
    req.body.introduce,
    req.body.original_price,
    req.body.current_price,
    req.body.user_id
  ], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '已添加到浏览记录！' }));
  });
});
//查看浏览记录
router.post('/selBrowsing', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 6;
  let end = page * 6;
  mysql.blog(sql.SelBrowsing, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//删除浏览记录
router.post('/delBrowsing', function (req, res, next) {
  mysql.blog(sql.delBrowsing, [req.body.id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '又偷偷看什么了？？？' }));
  });
});
//我的消息
router.post('/news', function (req, res, next) {
  mysql.blog(sql.news, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
router.post('/selNews', function (req, res, next) {
  mysql.blog(sql.selNews, [req.body.message_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '看过了兄弟！！！' }));
  });
});
router.post('/delNews', function (req, res, next) {
  mysql.blog(sql.delNews, [req.body.message_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '已经删除了！！！' }));
  });
});
//反馈
router.post('/feedback', function (req, res, next) {
  mysql.blog(sql.feedback, [req.body.opinion, req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '您的意见我们已经收到!会酌情修改' }));
  });
});
module.exports = router;